﻿<%@ WebHandler Language="C#" Class="delfile" %>

using System;
using System.Web;
using System.IO;
using System.Configuration;

public class delfile : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";  
          string file = context.Request.Params["file"];
          string id = context.Request.Params["id"];
          string img = context.Request.Params["img"];
          string image_id = context.Request.Params["image_id"];
		  
          if (!string.IsNullOrEmpty(img))
          {
              context.Response.Write(cmddefile(img));
          }
          if (!string.IsNullOrEmpty(image_id))
          {
              context.Response.Write(cmddefile(image_id));
          }		  
    }

    public string dirPath = ConfigurationManager.AppSettings["dirPath"].ToString();
	public string sitampdirPath = ConfigurationManager.AppSettings["sitampdirPath"].ToString();
    
	Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public string cmddefile(string id, string basePath = "~/Upload/Images/")
    {
        basePath = (basePath.IndexOf("~") > -1) ? HttpContext.Current.Server.MapPath("~") :
        basePath;
        string FilePath = "";
        FilePath = mssql.GetString("select rtrim(url) as url from [ImageDoc](nolock) where imageid='"+id+"'");
        mssql.ExecuteSql("delete from [ImageDoc] where imageid='" + id + "'");
        if (File.Exists(basePath + FilePath.Replace("upload/","")))
        {            
            File.Delete(basePath + FilePath);
            return "{\"code\":1,\"msg\":\"删除成功1\",\"image_id\":\"" + id + "\",\"Path\":\"" + FilePath + "\"}";
        }
        else
        {
            if (File.Exists(dirPath + FilePath.Replace("Upload/","")))
            {
                File.Delete(dirPath + FilePath.Replace("Upload/",""));
                return "{\"code\":1,\"msg\":\"删除成功2\",\"image_id\":\"" + id + "\",\"Path\":\"" + FilePath + "\"}";
            }
            else
            {
				if (File.Exists(sitampdirPath + FilePath.Replace("Upload/","")))
				{
					File.Delete(sitampdirPath + FilePath.Replace("Upload/",""));
					return "{\"code\":1,\"msg\":\"删除成功3\",\"image_id\":\"" + id + "\",\"Path\":\"" + FilePath + "\"}";
				}				
				else
				{					
					//return "{\"code\":1,\"msg\":\"删除成功\",\"image_id\":\"" + id + basePath + "|" + dirPath + "|" + sitampdirPath +  "|" + FilePath + "\"}";
					return "{\"code\":1,\"msg\":\"删除成功4\",\"image_id\":\"" + id + "\",\"Path\":\"\"}";
					
				}
				
            }
        }
    }    
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}